package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Intent;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarriageHistoryBinding;
import com.sgb.kjwl.model.entity.self.CarriagePushEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.CarriagePushHistoryAdapter;
import com.sgb.kjwl.viewmodel.self.carMakers.CarriageHistoryModel;
import com.sgb.link.router.PathLogistics;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.util.ArrayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 10:24
 * @Description: 历史承运
 */
@Route(path = PathLogistics.CarriageHistoryActivity)
public class CarriageHistoryActivity extends AppActivity {

    private CarriageHistoryModel mViewModel;
    private ActivityCarriageHistoryBinding mBinding;
    //
    private List<CarriagePushEntity> mList;
    private CarriagePushHistoryAdapter mAdapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        mViewModel = new ViewModelProvider(this).get(CarriageHistoryModel.class);
    }

    @Override
    protected void init() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_carriage_history);
        mBinding.setLifecycleOwner(this);
        mBinding.includeTitle.titleView.setText("选择承运");
        initRecycler();
        initEvent();
        initData();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CarriagePushHistoryAdapter(mList);
        mBinding.rvPushCarriage.setLayoutManager(new LinearLayoutManager(this));
        DividerDecoration.builder(this)
                .colorRes(R.color.transparent)
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .showLastDivider()
                .build()
                .addTo(mBinding.rvPushCarriage);
        mBinding.rvPushCarriage.setAdapter(mAdapter);
    }

    private void initEvent() {
        mBinding.includeTitle.ivBack.setOnClickListener(this);
        mBinding.srlPushCarriage.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getList(CarriageHistoryActivity.this, false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getList(CarriageHistoryActivity.this, true);
            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CarriagePushEntity entity = mList.get(position);
            Intent intent = new Intent();
            intent.putExtra(RouterKey.NO, entity.getNo());
            setResult(RESULT_OK, intent);
            finish();
        });
        //接口回调
        mViewModel.ldList.observe(this, this::syncList);
    }

    private void initData() {
        mViewModel.getList(this, true);
    }

    /**
     * 同步列表数据
     */
    private void syncList(ListResponseV1<List<CarriagePushEntity>> listResponse) {
        mBinding.srlPushCarriage.finishRefresh();
        mBinding.srlPushCarriage.finishLoadMore();
        if (!listResponse.loadSuccess()) {
            if (listResponse.getPageNumber() == 1) {
                mAdapter.setEmptyView(R.layout.layout_null);
            }
        }
        if (listResponse.getPageNumber() == 1) {
            mList.clear();
        }
        if (listResponse.getList() != null) {
            mList.addAll(listResponse.getList());
        }
        mAdapter.notifyDataSetChanged();
        //
        mBinding.srlPushCarriage.setNoMoreData(!listResponse.haveMoreData());
        //设置空布局
        if (ArrayUtil.isNullOrEmpty(mList)) {
            View empty = LayoutInflater.from(this).inflate(R.layout.layout_null,null, false);
            mAdapter.setEmptyView(empty);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.includeTitle.ivBack) {
            finish();
        }
    }
}
